<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Core\BaseController;
use App\Models\Member;
use App\Models\Sms;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Input;
use function MongoDB\BSON\toJSON;

class RegisterController extends BaseController
{
    //注册第一步
    public function step1(Request $request)
    {
        if ($request->method() == 'POST') {
            $mobile = $request->get('mobile');
            $password = $request->get('password');
            $code = $request->get('code');
            //判断是否验证码有效
            $sms = Sms::where(['mobile' => $mobile, 'code' => $code])->where('create_time', '>', time() - 600)->get();
            if (!$sms->first()) {
                return $this->retRequestJson(11, '验证码无效');
            }
            //判断账号是否已经注册
            $member = Member::where(['username' => $mobile])->get();
            if ($member->first()) {
                return $this->retRequestJson(12, '该手机号已经注册,您可以直接登录，或者更换其他手机号码');
            }
            $password = password_hash($password, PASSWORD_DEFAULT);
            $model = new Member();
            $model->username = $mobile;
            $model->mobile = $mobile;
            $model->password = $password;
            if ($model->save()) {
                return $this->retRequestJson(0, '成功', ['url' => url()]);
            } else {
                return $this->retRequestJson(10, '该手机号已经注册');
            }

        }
        return view('home.auth.register');
    }

    public function step2()
    {
        return view('home.auth.step2');
    }

}